<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Ae_Login extends Controller {

	public function action_index()
	{
		$msg = '';
		if ($this->request->method() == 'POST'){
			$subdata = $this->request->post();
			if (!empty($subdata['username']) && !empty($subdata['password'])){
					
				$password = trim($subdata['password']);
				$username = trim($subdata['username']);
				//$remember = (isset($subdata['remember']) && $subdata['remember'] == 'on') ? true : false;
				
				$Museradmin = Model::factory('Useradmin');
				$re = $Museradmin->check($username, $password);
				if (empty($re)){
					$msg = '用户名或密码错误，请重新输入。';
				}else {
					// 设置用户session
					$uid = isset($re[0]['uid']) ? $re[0]['uid'] : 0;
					$Session = Session::instance();
					$Session->set('admin_uid', $uid);
					$Session->set('admin_username', $username);
					$Session->set('admin_login_time', date('Y-m-d H:i:s'));
					
					// 更新最后登录信息
					$data = array(
							'logintime' => date('Y-m-d H:i:s'),
							'loginip' => Request::$client_ip
						);
					$re = $Museradmin->update($data, $uid);
					if ($re){
						$Museradmin->update($data, $uid);
					}
					
					header('location: /ae/index');
					exit;
				}
			}else {
				$msg = '请输入用户名和密码。';
			}
		}
		
		$view = View::factory('ae/login');
		$view->bind('msg', $msg);
		$this->response->body($view);
	}

}
